package com.hxgis.web.controller;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.hxgis.common.base.ResultVO;
import com.hxgis.common.base.StationCache;
import com.hxgis.common.dto.AreaInfoDTO;
import com.hxgis.common.dto.StationInfoDTO;
import com.hxgis.common.entity.Station;
import com.hxgis.common.repository.StationInfoRepository;
import com.hxgis.common.service.StationService;
import com.hxgis.common.utils.GridUtil;
import com.hxgis.common.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 初始化站点信息
 */
@RestController
@RequestMapping("/station")
@Slf4j
@Api(tags = "station")
public class StationController {


    @Autowired
    StationService stationService;

    @Autowired
    private StationInfoRepository stationInfoRepository;


    @ApiOperation(value="查询所有的站点信息", notes="查询所有的站点信息")
    @GetMapping("get/allStation")
    @ResponseBody
    public ResultVO getAllStation(){
        return ResultVOUtil.success(stationService.getAllStation());
    }


    @ApiOperation(value="查询所有省份市信息", notes="查询所有省份市信息")
    @GetMapping("get/province")
    @ResponseBody
    public ResultVO getProvinceInfo(){
        Map<String, List<AreaInfoDTO>> areaInfosMap = stationService.getProvinceInfo();
        return ResultVOUtil.success(areaInfosMap);
    }


    @ApiOperation(value="将基础信息文件导入索引", notes="将基础信息文件导入索引")
    @ApiImplicitParam(name = "file", value = "文件名", required = true, dataType = "file")
    @PostMapping("/batch/import")
    @ResponseBody
    public ResultVO importExcel2(@RequestParam("file") MultipartFile file) {
        ImportParams importParams = new ImportParams();
        // 数据处理
        importParams.setHeadRows(1);
        importParams.setTitleRows(0);
        // 需要验证
        importParams.setNeedVerfiy(false);
        try {
            ExcelImportResult<Station> result = ExcelImportUtil.importExcelMore(file.getInputStream(), Station.class,
                    importParams);
            List<Station> stations = result.getList();
            stations.stream().forEach(x -> {
                x.setGridNum(GridUtil.getGridByLonAndLat(Double.parseDouble(x.getLon()),Double.parseDouble(x.getLat())));
            });
            stationInfoRepository.saveBatchNotNull(stations);
//            stations.stream().forEach(x -> {
//                log.info("从Excel导入数据到数据库的详细为 ：{}", JSONObject.toJSONString(x));
//                //将文件导入索引
//                stationInfoRepository.save(x);
//            });
            log.info("从Excel导入数据一共 {} 行 ", stations.size());
        } catch (IOException e) {
            log.error("导入失败：{}", e.getMessage());
        } catch (Exception e1) {
            log.error("导入失败：{}", e1.getMessage());
        }
        return ResultVOUtil.success("导入成功");
    }



    @ApiOperation(value="初始化站点距离小于200km的数据", notes="初始化站点距离小于200km的数据")
    @GetMapping("initDistance200ToEs")
    public ResultVO initDistance200ToEs(){
        stationService.initDistance200ToEs();
        return ResultVOUtil.success();
    }



}
